package com.xinthe.krisco.service;

import java.io.File;
import java.io.IOException;

import java.rmi.RemoteException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import com.xinthe.krisco.consts.KriscoConstants;
import com.xinthe.krisco.domain.OrderTo;
import com.xinthe.krisco.utils.ValidationUtil;
import com.xinthe.krisco.utils.XintheUtil;

/**
 * 
 * @author Sridher
 * 
 */
public class BeforeCreateOrderWrapper {

	/*
	 * location for generated XMl file
	 */
	public static final String xmlFilePath = "E:/oms-practice-workspace/krisco/outputfiles/generatedXmlFile.xml";

	public static final String createxmlFile = "D:/oms-practice-workspace/krisco/outputfiles/createXmlFile.xml";

	private static Document doc;

	/**
	 * @param env
	 * @param indoc
	 * @return
	 * @throws YFSException
	 * @throws YIFClientCreationException
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */

	/**
	 * Form input CreateOrder Xml and invoke the CreateOrder()API
	 * 
	 * @param env
	 * @param inCreateOrderdoc
	 * @return an outCreateOrdeDoc as document to the caller of the method.
	 * @throws YIFClientCreationException
	 * @throws RemoteException
	 * @throws YFSException
	 * 
	 */

	OrderTo orderAttributes;

	public static void main(String[] args) {
		CreateOrder(doc);
	}

	public static Document CreateOrder(Document inCreateOrderdoc)

	{

		try {
			Document createOrder = XintheUtil.createNewDocument("Order");
			Element rootEle = createOrder.getDocumentElement();
			rootEle.setAttribute(KriscoConstants.BUYER_USER_ID.getValue(),
					"Team1_Marks");
			rootEle.setAttribute(KriscoConstants.DOUCUMNET_TYPE.getValue(),
					"0001");
			rootEle.setAttribute(KriscoConstants.ENTERPRISE_CODE.getValue(),
					"Team1_KRISCO");
			rootEle.setAttribute(KriscoConstants.ORDER_DATE.getValue(),
					ValidationUtil.generateOrderDate());
			rootEle.setAttribute(KriscoConstants.ORDER_NO.getValue(),
					ValidationUtil.generateOrderNo());
			rootEle.setAttribute(
					KriscoConstants.SELLERORGANIZATION_CODE.getValue(),
					"Team1_Sopies ");

			Element eleOrderLines = XintheUtil.createChild(rootEle,
					"OrderLines");
			Element eleOrderLine = XintheUtil.createChild(eleOrderLines,
					"OrderLine");

			eleOrderLine.setAttribute(
					KriscoConstants.FULFILLMENT_TYPE.getValue(), "");
			eleOrderLine.setAttribute(KriscoConstants.ORDERED_QTY.getValue(),
					"");
			eleOrderLine.setAttribute(KriscoConstants.PRIMELINE_NO.getValue(),
					"1");
			eleOrderLine.setAttribute(KriscoConstants.SUBLINE_NO.getValue(),
					"2");

			Element eleItem = XintheUtil.createChild(eleOrderLine, "Item");

			eleItem.setAttribute(KriscoConstants.ITEM_ID.getValue(),
					"Team1_Eagle");
			eleItem.setAttribute(KriscoConstants.ITEM_SHORT_DESC.getValue(),
					"It's Eagle Item");
			eleItem.setAttribute(KriscoConstants.PRODUCT_CLASS.getValue(),
					"GOOD");
			eleItem.setAttribute(KriscoConstants.UNIT_OF_MEASURE.getValue(),
					"EACH");
			eleItem.setAttribute(KriscoConstants.COUNTRY_OF_ORIGIN.getValue(),
					"");

			Element elePersonInfoShipTo = XintheUtil.createChild(eleOrderLine,
					"PersonInfoShipTo");

			elePersonInfoShipTo.setAttribute(
					KriscoConstants.OTHER_PHONE.getValue(), "");
			elePersonInfoShipTo.setAttribute(
					KriscoConstants.DAY_PHONE.getValue(), "");
			elePersonInfoShipTo.setAttribute(
					KriscoConstants.EMAIL_ID.getValue(), "");
			elePersonInfoShipTo.setAttribute(
					KriscoConstants.MIDDLE_NAME.getValue(), "");
			elePersonInfoShipTo.setAttribute(
					KriscoConstants.FIRST_NAME.getValue(), "");
			elePersonInfoShipTo.setAttribute(
					KriscoConstants.LAST_NAME.getValue(), "");
			elePersonInfoShipTo.setAttribute(
					KriscoConstants.COUNTRY.getValue(), "");
			elePersonInfoShipTo.setAttribute(
					KriscoConstants.ZIP_CODE.getValue(), "");
			elePersonInfoShipTo.setAttribute(KriscoConstants.STATE.getValue(),
					"");
			elePersonInfoShipTo.setAttribute(KriscoConstants.CITY.getValue(),
					"");
			elePersonInfoShipTo.setAttribute(
					KriscoConstants.ADDRESS_LINE1.getValue(), "");
			elePersonInfoShipTo.setAttribute(
					KriscoConstants.ADDRESS_LINE2.getValue(), "");
			elePersonInfoShipTo.setAttribute(
					KriscoConstants.ADDRESS_LINE3.getValue(), "");

			Element eleLinePriceInfo = XintheUtil.createChild(eleOrderLine,
					"LinePriceInfo");

			eleLinePriceInfo.setAttribute(
					KriscoConstants.ISPRICE_LOCKED.getValue(), "Y");
			eleLinePriceInfo.setAttribute(
					KriscoConstants.LIST_PRICE.getValue(), "");
			eleLinePriceInfo.setAttribute(
					KriscoConstants.UNIT_PRICE.getValue(), "");

			Element eleInstructions = XintheUtil.createChild(rootEle,
					"Instructions");
			Element eleInstruction = XintheUtil.createChild(eleInstructions,
					"Instruction");

			eleInstruction.setAttribute(
					KriscoConstants.INSTRUCTION_TEXT.getValue(),
					"Leave it next door");
			eleInstruction.setAttribute(
					KriscoConstants.INSTRUCTION_TYPE.getValue(), "");
			eleInstruction.setAttribute(KriscoConstants.SEQUENCE_NO.getValue(),
					"1");

			Element elePersonInfoBillTo = XintheUtil.createChild(rootEle,
					"PersonInfoBillTo");

			elePersonInfoBillTo.setAttribute(
					KriscoConstants.OTHER_PHONE.getValue(), "");
			elePersonInfoBillTo.setAttribute(
					KriscoConstants.DAY_PHONE.getValue(), "");
			elePersonInfoBillTo.setAttribute(
					KriscoConstants.EMAIL_ID.getValue(), "");
			elePersonInfoBillTo.setAttribute(
					KriscoConstants.MIDDLE_NAME.getValue(), "");
			elePersonInfoBillTo.setAttribute(
					KriscoConstants.FIRST_NAME.getValue(), "");
			elePersonInfoBillTo.setAttribute(
					KriscoConstants.LAST_NAME.getValue(), "");
			elePersonInfoBillTo.setAttribute(
					KriscoConstants.COUNTRY.getValue(), "");
			elePersonInfoBillTo.setAttribute(
					KriscoConstants.ZIP_CODE.getValue(), "");
			elePersonInfoBillTo.setAttribute(KriscoConstants.STATE.getValue(),
					"");
			elePersonInfoBillTo.setAttribute(KriscoConstants.CITY.getValue(),
					"");
			elePersonInfoBillTo.setAttribute(
					KriscoConstants.ADDRESS_LINE1.getValue(), "");
			elePersonInfoBillTo.setAttribute(
					KriscoConstants.ADDRESS_LINE2.getValue(), "");
			elePersonInfoBillTo.setAttribute(
					KriscoConstants.ADDRESS_LINE3.getValue(), "");

			Element eleHeaderCharges = XintheUtil.createChild(rootEle,
					"HeaderCharges");

			Element eleHeaderCharge = XintheUtil.createChild(eleHeaderCharges,
					"HeaderCharge");
			eleHeaderCharge.setAttribute(
					KriscoConstants.CHARGE_CATEGORY.getValue(), "");
			eleHeaderCharge.setAttribute(
					KriscoConstants.CHARGE_NAME.getValue(), "Charge");
			eleHeaderCharge.setAttribute(
					KriscoConstants.CHARGE_AMOUNT.getValue(), "");

			Element eleHeaderCharg = XintheUtil.createChild(eleHeaderCharges,
					"HeaderCharge");
			eleHeaderCharg.setAttribute(
					KriscoConstants.CHARGE_CATEGORY.getValue(), "");
			eleHeaderCharg.setAttribute(KriscoConstants.CHARGE_NAME.getValue(),
					"Tax");
			eleHeaderCharg.setAttribute(
					KriscoConstants.CHARGE_AMOUNT.getValue(), "");

			System.out.println("Done building CreateOrder XML \n"
					+ XintheUtil.getXmlString(createOrder));

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return inCreateOrderdoc;
	}

	public static Document paymentDetails(Document outCreateOrdeDoc) {

		try {

			Document PaymentMethods = XintheUtil
					.createNewDocument("PaymentMethods");
			Element elePaymentMethods = PaymentMethods.getDocumentElement();
			Element elePaymentMethod = XintheUtil.createChild(
					elePaymentMethods, "PaymentMethod");

			elePaymentMethod.setAttribute(
					KriscoConstants.PAYMENT_REFERENCE2.getValue(),
					"PayRef0000002");
			elePaymentMethod.setAttribute(
					KriscoConstants.PAYMENT_REFERENCE1.getValue(),
					"PayRef0000001");
			elePaymentMethod.setAttribute(
					KriscoConstants.CREDITCARD_EXPDATE.getValue(), "");
			elePaymentMethod.setAttribute(
					KriscoConstants.CREDITCARD_NAME.getValue(), "Tony Stark");
			elePaymentMethod.setAttribute(
					KriscoConstants.CREDITCARD_NO.getValue(),
					"11111111111111112");
			elePaymentMethod.setAttribute(
					KriscoConstants.CREDITCARD_TYPE.getValue(), "VISA");
			elePaymentMethod.setAttribute(
					KriscoConstants.DISPLAY_CREDITCARD_NO.getValue(), "1112");
			elePaymentMethod.setAttribute(
					KriscoConstants.MAXCHARGE_LIMIT.getValue(), "113.50");
			elePaymentMethod.setAttribute(
					KriscoConstants.PAYMENT_TYPE.getValue(), "CREDIT_CARD");
			elePaymentMethod.setAttribute(
					KriscoConstants.UNLIMITED_CHARGES.getValue(), "Y");

			Element elePaymentDetailsList = XintheUtil.createChild(
					elePaymentMethod, "PaymentDetailsList");
			Element elePaymentDetails = XintheUtil.createChild(
					elePaymentDetailsList, "PaymentDetails");

			elePaymentDetails.setAttribute(
					KriscoConstants.AUTHORIZATION_EXPIRATION_DATE.getValue(),
					"20130915T12:00:00");
			elePaymentDetails.setAttribute(
					KriscoConstants.CHARGE_TYPE.getValue(), "AUTHORIZATION");
			elePaymentDetails.setAttribute(
					KriscoConstants.PROCESSED_AMOUNT.getValue(), "25.00");
			elePaymentDetails.setAttribute(
					KriscoConstants.AUTHORIZATION_ID.getValue(),
					"EPSAuthID00000001");

			// Transform the DOM Object to an XML File

			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();

			Transformer transformer = transformerFactory.newTransformer();

			DOMSource domSource = new DOMSource(PaymentMethods);

			StreamResult streamResult = new StreamResult(new File(xmlFilePath));

			transformer.transform(domSource, streamResult);

			System.out.println("Done creating XML File");

		} catch (ParserConfigurationException pce) {

			pce.printStackTrace();

		} catch (TransformerException tfe) {

			tfe.printStackTrace();

		} catch (Exception e) {

			e.printStackTrace();
		}

		return outCreateOrdeDoc;

	}

	public static int itemQuantity(int quantity) {
		OrderTo order = new OrderTo();
		order.getQuantity();

		return quantity;

	}

}